package com.zzq.base.awesome.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzq.base.awesome.api.entity.User;
import com.zzq.base.awesome.api.mapper.UserMapper;
import com.zzq.base.awesome.api.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.base.awesome.utils.StringTypeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵自强
 * @since 2020-02-09
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public User getUserOnLogin(String userName) {
        User user = null;
        if (StringTypeUtil.checkPhone(userName)) {
            user = this.getOne(new QueryWrapper<User>().lambda().eq(User::getTelephone, userName));
            log.info("根据用户手机号获取用户信息：" + user);
        } else if (StringTypeUtil.isEmail(userName)) {
            user = this.getOne(new QueryWrapper<User>().lambda().eq(User::getEmail, userName));
            log.info("根据用户邮箱获取用户信息：" + user);
        } else {
            user = this.getOne(new QueryWrapper<User>().lambda().eq(User::getAccount, userName));
            log.info("根据用户帐号获取用户信息：" + user);
        }
        return user;
    }

    @Override
    public boolean isPasswordOk(User user, String password) {
        return user.getPassword().equals(password);
    }

    @Override
    public boolean isCanRegister(String account, String mail, String password, String mobile,String userName) {
        if (StringUtils.isEmpty(account)) {
            return false;
        }
        if (StringUtils.isEmpty(password)) {
            return false;
        }
        if (StringUtils.isEmpty(userName)) {
            return false;
        }
        if (!StringTypeUtil.isEmail(mail)) {
            log.info("不是邮箱");
            return false;
        }
        if (!StringTypeUtil.checkPhone(mobile)) {
            log.info("不是手机号");
            return false;
        }
        return true;
    }

    @Override
    public boolean haveSameUser(String account, String mail, String mobile) {
        List<User> u = this.list(new QueryWrapper<User>()
                .lambda()
                .eq(User::getAccount, account)
                .or()
                .eq(User::getEmail, mail)
                .or()
                .eq(User::getTelephone, mobile)
        );
        return u.size() > 0;
    }

    @Override
    public boolean canUpdate(int id, String account, String mail, String mobile) {
        List<User> u = this.list(new QueryWrapper<User>()
                .lambda()
                .eq(User::getAccount, account)
                .or()
                .eq(User::getEmail, mail)
                .or()
                .eq(User::getTelephone, mobile)
        );
        if (u.size() == 0) {
            return true;
        } else if (u.size() == 1 && u.get(0).getId() == id) {
            return true;
        }else {
            return false;
        }
    }
}
